package com.control.qct.businesssteward.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.model.GoodsDetailModel;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 图片放大
 * Created by Administrator on 2018/3/21 0021.
 */

public class PhotoViewPagerActivity extends BaseActivity {
    @BindView(R.id.view_pager)
    ViewPager mViewPager;


    private ArrayList<GoodsDetailModel.FileImageUrl> mPhotos;//选择的图片地址集合
    private int mPhotoId;

    public PhotoViewPagerActivity() {
        super(R.layout.activity_photo_viewpager);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mPhotos = (ArrayList<GoodsDetailModel.FileImageUrl>) extras.getSerializable("photo_address");
        mPhotoId = extras.getInt("photoId");
    }

    @Override
    protected void initView() {
        mViewPager.setAdapter(new PhotoPagerAdapter());
        mViewPager.setCurrentItem(mPhotoId);
    }

    class PhotoPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPhotos.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            Glide.with(PhotoViewPagerActivity.this).load(mPhotos.get(position).getImageBigUrl()).into(photoView);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
